package com.zhuiyun.project.api.article.mapper;

import com.zhuiyun.project.api.article.entity.Article;
import com.zhuiyun.project.api.article.entity.ArticleAudit;
import com.zhuiyun.project.api.article.model.ArticleAuditModel;
import com.zhuiyun.project.common.mapper.BaseMapper;
import com.zhuiyun.project.util.pageUtil.PageRequest;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleReviewMapper extends BaseMapper<Article> {

    /**查询发不完待审核的文章
    * @Author mao
    * @Description TODO 后台管理 获取待审核文章
    * @Date 2023/6/19 20:43
    * @Return java.util.List<com.zhuiyun.project.api.article.entity.Article>
    * @param pageRequest
    * @param userName
    * @param articleTitle
    */
    //查询待审核文章 分页 根据医师 文章标题进行模糊查询
//    @Select("<script>SELECT * FROM article aa LEFT JOIN app_user bb ON aa.user_id = bb.id WHERE aa.approval_status = 0 " +
//            "<when test='userName != null and userName != &quot;&quot;'> " +
//            "AND bb.user_name LIKE CONCAT('%', #{userName}, '%') " +
//            "</when> " +
//            "<when test='articleTitle != null and articleTitle != &quot;&quot;'> " +
//            "AND aa.article_title LIKE CONCAT('%', #{articleTitle}, '%') " +
//            "</when> " +
//            " </script>")
    @Select("<script>SELECT * FROM article aa LEFT JOIN app_physician phy on phy.id=aa.user_id left join " +
            "app_user bb ON phy.user_id = bb.id LEFT JOIN " +
            "sys_dict_data cc ON aa.label_id = cc.data_id " +
            "WHERE aa.approval_status = 0 " +
            "<when test='userName != null and userName != &quot;&quot;'> " +
            "AND bb.user_name LIKE CONCAT('%', #{userName}, '%') " +
            "</when> " +
            "<when test='articleTitle != null and articleTitle != &quot;&quot;'> " +
            "AND aa.article_title LIKE CONCAT('%', #{articleTitle}, '%') " +
            "</when> ORDER BY aa.publish_time desc " +
            " </script>")
    List<Article> getArticle(@Param("pageRequest") PageRequest pageRequest,
                             @Param("userName") String userName,
                             @Param("articleTitle") String articleTitle);



    /**
     *改变审核状态
     * @author 温
     * @date 2023/7/17 18:43
     * @parameter [id]
     * @return int
     */
    @Update("update  article set approval_status=1,is_published=1 where id=#{id} ")
    int updateArticleStatus(Integer id);


    /**
     * 文章审核通过
     * @author 温
     * @date 2023/7/17 18:43
     * @parameter [articleAudit]
     * @return int
     */
    @Insert("insert into article_audit (article_id,audit_time,auditor,audit_status,audit_result,audit_comment,create_time,update_time) values(#{id},now(),#{auditor},1,1,#{auditComment},now(),now())")
    int addArticleStatus(ArticleAudit articleAudit);



    /**
     * 改变审核状态
     * @param id
     * @return
     */
    @Update("update article set approval_status=2 where id=#{id}")
    int updateArticleStatuss(Integer id);

/**
 * 文章审核驳回
 * @author 温
 * @date 2023/7/17 19:43
 * @parameter [ArticleAudit]
 * @return int
 */
    @Insert("insert into article_audit (article_id,audit_time,auditor,audit_status,audit_result,audit_comment,create_time,update_time) values(#{id},now(),#{auditor},1,1,#{auditComment},now(),now())")
    int addArticleUpdate(ArticleAudit articleAudits);


    /**
     * 审核记录查询
     * @author 温
     * @date 2023/7/18 11:17
     * @parameter [articleTitle, pageRequest]
     * @return java.util.List<com.zhuiyun.project.api.article.model.ArticleAuditModel>
     */
    @Select("select * from article_audit aa  LEFT JOIN  article bb on aa.article_id=bb.id left join " +
            "app_physician phy on phy.id=bb.user_id left join " +
            "app_user user ON phy.user_id = user.id  " +
            "LEFT JOIN  sys_dict_data cc on bb.label_id=cc.data_id " +
            "where article_title like '%${articleTitle}%' ORDER BY aa.audit_time desc")
    List<ArticleAuditModel>ArticleAuditList(@Param("articleTitle") String articleTitle,@Param("PageRequest") PageRequest pageRequest);


}

